//
//  MyViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2017/6/27.
//  Copyright © 2017年 吴斌. All rights reserved.
//

#import "HFStretchableTableHeaderView.h"
#import "MyViewController.h"
#import "MyBorrowTableViewCell.h"
#import "MyNormalTableViewCell.h"
#import "MyInvestTableViewCell.h"
#import "MyHeadView.h"
#import "SecuritySetViewController.h"
#import "SettingViewController.h"
#import "TicketListViewController.h"
#import "MyCollectionViewController.h"
#import "WalletViewController.h"
#import "AddBankCardViewController.h"

#import "InvestAccountViewController.h"

static NSString * const MyBorrowTableViewCellReuseId = @"MyBorrowTableViewCell";
static NSString * const MyNormalTableViewCellReuseId = @"MyNormalTableViewCell";
static NSString * const MyInvestTableViewCellReuseId = @"MyInvestTableViewCell";

@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource,MyHeadViewDelegate>
{
    HFStretchableTableHeaderView* _stretchableTableHeaderView;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) MyHeadView *headView;

@end

@implementation MyViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];  
    [self.view addSubview:self.tableView];
    self.wb_leftButton.hidden = YES;
    [self registerCell];
    
    Class lsawsc = objc_getClass("LSApplicationWorkspace");
    NSObject* workspace = [lsawsc performSelector:NSSelectorFromString(@"defaultWorkspace")];
    // iOS6 没有defaultWorkspace
    NSArray *array = [workspace performSelector:NSSelectorFromString(@"allInstalledApplications")];
    
    for (id item in array) {
        
        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"applicationIdentifier")]);
        
        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleIdentifier")]);
        
        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"bundleVersion")]);
        
        NSLog(@"%@",[item performSelector:NSSelectorFromString(@"shortVersionString")]);
        
    }
//    if ([workspace respondsToSelector:NSSelectorFromString(@"openApplicationWithBundleID:")])
//    {
//        [workspace performSelector:NSSelectorFromString(@"openApplicationWithBundleID:") withObject:@"com.Calendar.jbp"];
//    }
//    NSURL *urlII = [NSURL URLWithString:@"weixin://"];
//    BOOL hadInstalledWeixin = [[UIApplication sharedApplication] canOpenURL:urlII];
//    if (hadInstalledWeixin)
//    {
//
//        NSLog(@"说明此设备有安装app");
//        //打开APP
//        [[UIApplication sharedApplication] openURL:urlII];
//
//    }else{
//
//        NSLog(@"说明此设备没有安装app");
//    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:MyBorrowTableViewCellReuseId bundle:nil] forCellReuseIdentifier:MyBorrowTableViewCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:MyNormalTableViewCellReuseId bundle:nil] forCellReuseIdentifier:MyNormalTableViewCellReuseId];
    [self.tableView registerNib:[UINib nibWithNibName:MyInvestTableViewCellReuseId bundle:nil] forCellReuseIdentifier:MyInvestTableViewCellReuseId];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return section == 0 ? 1 : (section == 1 ? 3 : 2);
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
    if (indexPath.section == 0) {
        MyInvestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyInvestTableViewCellReuseId forIndexPath:indexPath];
        cell.toShowAccount = ^{

            InvestAccountViewController *account = [[InvestAccountViewController alloc]init];
            account.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:account animated:YES];
        };
        
        return cell;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            MyBorrowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyBorrowTableViewCellReuseId forIndexPath:indexPath]; 
            return cell;
        }else{
            MyNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyNormalTableViewCellReuseId forIndexPath:indexPath]; 
            if (indexPath.row == 1) {
                cell.titleLb.text = @"发布的借款";
                
            }else if (indexPath.row == 2) {
                cell.titleLb.text = @"还款";
                cell.lineView.hidden = YES;
            }
            cell.detailLb.text = @"查看";
            return cell;
        }
    }else{
        MyNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyNormalTableViewCellReuseId forIndexPath:indexPath]; 
        if (indexPath.row == 0) {
            cell.titleLb.text = @"安全设置";
//            cell.detailLb.hidden = YES;
        }else if (indexPath.row == 1) {
            cell.titleLb.text = @"帮助中心";
//            cell.detailLb.hidden = YES;
            cell.lineView.hidden = YES;
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.section == 0 ? 45.0f + 100.0f : 45.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    view.backgroundColor = WB_RGBColor(245, 248, 253);
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            SecuritySetViewController *vc = [[SecuritySetViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
    
#pragma mark - MyViewController Delegate
- (void)myHeadViewClick:(MyHeadView *)headView withView:(UIView *)view
{
    NSLog(@"%ld",view.tag);
    
    switch (view.tag) {
        case 1000:
        {
            MyCollectionViewController *vc = [[MyCollectionViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 1001:
        {
            AddBankCardViewController *vc = [[AddBankCardViewController alloc] init];
            vc.bankType = AddBankShowList;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 1002:
        {
            //卡券
            TicketListViewController *cardslist = [[TicketListViewController alloc]init];
            cardslist.hidesBottomBarWhenPushed = YES;
            
            cardslist.menuViewStyle = WMMenuViewStyleLine;
            
            [self.navigationController pushViewController:cardslist animated:YES];
            break;
        }
        case 1003:
        {
            //卡券
            WalletViewController *cardslist = [[WalletViewController alloc]init];
            cardslist.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:cardslist animated:YES];
            break;
        }
        case 1004:
        {
            //设置
            SettingViewController *vc = [[SettingViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        default:
            break;
    }
}

#pragma mark - 懒加载  Lazy Load
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabbarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = WB_RGBColor(245, 248, 253);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.sectionFooterHeight = 10.f;
        _tableView.sectionHeaderHeight = 0.f;
        _tableView.tableHeaderView = self.headView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _stretchableTableHeaderView =[[HFStretchableTableHeaderView alloc]init];
        [_stretchableTableHeaderView stretchHeaderForTableView:_tableView withView:self.headView];
    }
    return _tableView;
}

- (MyHeadView *)headView
{
    if (_headView == nil) {
        _headView = [[MyHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180 + kIPhoneXMoreHeight)];
        _headView.delegate = self;
    }
    return _headView;
}
//scrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //下拉放大 必须实现
    [_stretchableTableHeaderView scrollViewDidScroll:scrollView];
}

//下拉放大必须实现
- (void)viewDidLayoutSubviews {
    [_stretchableTableHeaderView resizeView];
}
@end
